package reflectdemo;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 刘远望
 * Date: 2025-05-01
 * Time: 17:30
 */
//反射
public class Test {

    public static void reflectNewInstance() {
        try {
            Class<?> c1 = Class.forName("reflectdemo.Student");

            Student student = (Student) c1.newInstance();

            System.out.println(student);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public static void reflectPrivateConstructor() {
        try {
            Class<?> c1 = Class.forName("reflectdemo.Student");
            Constructor<Student> constructor =
                    (Constructor<Student>) c1.getDeclaredConstructor(String.class, int.class);

            constructor.setAccessible(true);

            Student student = constructor.newInstance("zhangsan", 18);
            System.out.println(student);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public static void reflectPrivateField() {
        try {
            Class<?> c1 = Class.forName("reflectdemo.Student");
            Field field = c1.getDeclaredField("name");

            field.setAccessible(true);

            Student student = (Student) c1.newInstance();

            //field.set(student, "poll");//改变名字
            System.out.println(student);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public static void reflectPrivateMethod() {
        try {
            Class<?> c1 = Class.forName("reflectdemo.Student");
            Method method = c1.getDeclaredMethod("func", String.class);
            method.setAccessible(true);

            Student student = (Student) c1.newInstance();
            method.invoke(student, "我是一个参数");

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        //reflectNewInstance();
        //reflectPrivateConstructor();
       reflectPrivateField();
       // reflectPrivateMethod();

    }

    public static void main1(String[] args) {
        try {
            Class<?> c1 = Class.forName("reflectdemo.Student");

            Class c2 = Student.class;

            Student student = new Student();
            Class<?> c3 = student.getClass();

            //class对象只有一个
            System.out.println(c1.equals(c2));
            System.out.println(c1.equals(c3));
            System.out.println(c3.equals(c2));


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
